package com.lfy.listen;

import com.lfy.dao.UserDao;
import com.lfy.domian.User;
import com.lfy.domian.testEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

import javax.annotation.Resource;

@Component
public class TestEventListener {
    @Resource
    UserDao userDao;

    @TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT ,classes= testEvent.class,fallbackExecution = true)
    @Transactional(propagation=Propagation.REQUIRED,rollbackFor = Throwable.class)
    @Async
    public void l(testEvent msg){
        System.out.println("111111111111111111111111111111111111");
        String msg1 = msg.getMsg();
        System.out.println("收到消息"+msg1);
        User user = new User();
        user.setPhone("222");
        user.setUserid("222");
        user.setPasswd("222");
        user.setSchool("222");
        User user1 = new User();
        user1.setPhone("222");
        user1.setUserid("333");
        user1.setPasswd("222");
        user1.setSchool("222");
        userDao.insertUser(user);
        userDao.insertUser(user1);
    }
}
